package lumingweihua.future.cn.lumingweihua.home.cheyuan;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.AMapUtils;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.SupportMapFragment;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.Polyline;
import com.amap.api.maps2d.model.PolylineOptions;
import com.amap.api.maps2d.overlay.DrivingRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;

import java.util.ArrayList;
import java.util.List;

import lumingweihua.future.cn.baselibgxh.base.BaseFragment;
import lumingweihua.future.cn.baselibgxh.utils.ArithmeticUtil;
import lumingweihua.future.cn.lumingweihua.R;

public class DistanceFragment extends BaseFragment implements AMap.OnMarkerClickListener,RouteSearch.OnRouteSearchListener{
	
	protected AMap aMap;

	@Override
	protected View initView(LayoutInflater inflater, ViewGroup container) {
		View viewRoot = View.inflate(getActivity(), R.layout.fragment_location_map, null);
		return viewRoot;
	}

	protected double startLat;
	protected double startLon;
	protected double endLat;
	protected double endLon;
	public void setDatas(double startLat,double startLon,double endLat,double endLon){
		this.startLat = startLat==0?34.39:startLat;
		this.startLon = startLon==0?114.45:startLon;
		this.endLat = endLat==0?34.39:endLat;
		this.endLon = endLon==0?114.45:endLon;

	}

	private RouteSearch mRouteSearch;
	@Override
	protected void initData(Bundle savedInstanceState) {
		aMap = ((SupportMapFragment) baseActivity
				.getSupportFragmentManager().findFragmentById(R.id.map))
				.getMap();

//		aMap.getUiSettings().setZoomControlsEnabled(false);//设置缩放按钮
//		aMap.getUiSettings().setScaleControlsEnabled(false);//设置比例尺子

		aMap.addMarker(new MarkerOptions()
				.position(new LatLng(startLat, startLon))
				.icon(BitmapDescriptorFactory
				.fromResource(R.drawable.btn_qidian)));

		aMap.addMarker(new MarkerOptions()
				.position(new LatLng(endLat, endLon))
				.icon(BitmapDescriptorFactory
						.fromResource(R.drawable.btn_zhongdian)));

		mRouteSearch = new RouteSearch(context);
		mRouteSearch.setRouteSearchListener(this);
		LatLonPoint mStartLatlng = new LatLonPoint(startLat, startLon);
		LatLonPoint mEndLatlng = new LatLonPoint(endLat, endLon);
		final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
				mStartLatlng, mEndLatlng);
		RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, RouteSearch.DrivingDefault, null,
				null, "");// 第一个参数表示路径规划的起点和终点，第二个参数表示驾车模式，第三个参数表示途经点，第四个参数表示避让区域，第五个参数表示避让道路
		mRouteSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询

//		float dis = AMapUtils.calculateLineDistance(new LatLng(startLat, startLon), new LatLng(endLat, endLon));
//
//		int startx = 10000;
//		if(dis<startx)
//			aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(ArithmeticUtil.div(startLat+endLat,2), ArithmeticUtil.div(startLon+endLon,2)), 13));
//		else if(dis<startx*2)
//			aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(ArithmeticUtil.div(startLat+endLat,2), ArithmeticUtil.div(startLon+endLon,2)), 12));
//		else if(dis<startx*4)
//			aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(ArithmeticUtil.div(startLat+endLat,2), ArithmeticUtil.div(startLon+endLon,2)), 11));
//		else if(dis<startx*8)
//			aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(ArithmeticUtil.div(startLat+endLat,2), ArithmeticUtil.div(startLon+endLon,2)), 10));
//		else if(dis<startx*16)
//			aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(ArithmeticUtil.div(startLat+endLat,2), ArithmeticUtil.div(startLon+endLon,2)), 9));
//		else if(dis<startx*32)
//			aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(ArithmeticUtil.div(startLat+endLat,2), ArithmeticUtil.div(startLon+endLon,2)), 8));
//		else if(dis<startx*64)
//			aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(ArithmeticUtil.div(startLat+endLat,2), ArithmeticUtil.div(startLon+endLon,2)), 7));
//		else if(dis<startx*128)
//			aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(ArithmeticUtil.div(startLat+endLat,2), ArithmeticUtil.div(startLon+endLon,2)), 6));
//		else if(dis<startx*256)
//			aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(ArithmeticUtil.div(startLat+endLat,2), ArithmeticUtil.div(startLon+endLon,2)), 5));
//		else if(dis<startx*512)
//			aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(ArithmeticUtil.div(startLat+endLat,2), ArithmeticUtil.div(startLon+endLon,2)), 4));
//		else// if(dis<startx*1024)
//			aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(startLat, startLon), 3));
//
//		List<LatLng> latLngs = new ArrayList<LatLng>();
//		latLngs.add(new LatLng(startLat,startLon));
//		latLngs.add(new LatLng(endLat,endLon));
//        PolylineOptions polylineOptions = new PolylineOptions();
//
//        polylineOptions.addAll(latLngs).width(15).color(Color.parseColor("#006de3"));
//        aMap.addPolyline(polylineOptions);
	}

	//view 转bitmap

	public static Bitmap convertViewToBitmap(View view) {

	    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

	    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

	    view.buildDrawingCache();

	    Bitmap bitmap = view.getDrawingCache();

	    return bitmap;

	}
	
	@Override
	protected void processClick(View view) {
	}

	@Override
	public boolean onMarkerClick(Marker marker) {
		return false;
	}

	@Override
	public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

	}

	@Override
	public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
		if (i == 1000) {
			if (driveRouteResult != null && driveRouteResult.getPaths() != null) {
				if (driveRouteResult.getPaths().size() > 0) {
//					mDriveRouteResult = driveRouteResult;
					final DrivePath drivePath = driveRouteResult.getPaths()
							.get(0);
					DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(context, aMap, drivePath, driveRouteResult.getStartPos(),
							driveRouteResult.getTargetPos());
					drivingRouteOverlay.removeFromMap();
					drivingRouteOverlay.setNodeIconVisibility(false);//隐藏转弯的节点
					drivingRouteOverlay.addToMap();
					drivingRouteOverlay.zoomToSpan();
//					aMap.zoomOut();
				}
			}
		}
	}

	@Override
	public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

	}

	@Override
	public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

	}
}
